package com.auto.survey.measurecontroller

import android.util.Log
import com.auto.survey.activity.monitoring.entity.MonitoringPoint
import com.auto.survey.measurecontroller.entity.MeasureTask
import com.auto.survey.util.taskqueue.BluetoothTaskListener
import com.auto.survey.util.taskqueue.BluetoothTaskQueue
import com.auto.survey.util.taskqueue.BluetoothTaskResult

class BluetoothTaskManager (
    private val listener: MeasureProcessListener?,
    private val controller: RefinedMeasureController
){

    private val TAG = BluetoothTaskManager::class.java.simpleName
    /** 蓝牙任务队列，用于管理和执行蓝牙通信任务 */
    private val bluetoothQueue = BluetoothTaskQueue<String>().apply {
        setListener(object : BluetoothTaskListener<String> {
            override fun onTaskStarted(taskId: String) {
                listener?.onTaskStarted(taskId)
            }

            override fun onTaskSuccess(taskId: String, params: Map<String, Any>) {
                Log.i(TAG, "BluetoothTaskManager === onTaskSuccess")
                listener?.onTaskSuccess(taskId, params)
                controller.handleTaskSuccess(taskId, params)
            }

            override fun onTaskFailed(taskId: String, reason: String) {
                Log.i(TAG, "BluetoothTaskManager === onTaskFailed")
                listener?.onTaskFailed(taskId, reason)
                controller.currentTask?.status = MeasureTask.TaskStatus.FAILED
            }

            override fun onQueueCompleted() {
                Log.i(TAG, "BluetoothTaskManager === onQueueCompleted")
                listener?.onQueueCompleted()
                controller.completeCurrentProcess()
            }

            override fun onQueueAborted(lastFailedTaskId: String, reason: String) {
                Log.i(TAG, "BluetoothTaskManager === onQueueAborted")
                listener?.onQueueAborted(lastFailedTaskId, reason)
                controller.currentTask?.status = MeasureTask.TaskStatus.FAILED
            }

            override fun onQueuePaused() {
                Log.i(TAG, "BluetoothTaskManager === onQueuePaused")
                listener?.onQueuePaused()
            }

            override fun onQueueResumed() {
                Log.i(TAG, "BluetoothTaskManager === onQueueResumed")
                listener?.onQueueResumed()
            }
        })
    }
    /**
     * 添加粗定位任务
     */
    fun addRoughPositionTask(point: MonitoringPoint, loop: Int, action: suspend () -> BluetoothTaskResult) {
        val taskId = "${MeasureConstants.TASK_PREFIX_ROUGH_POSITION}${point.pointCode}_LOOP$loop"
        bluetoothQueue.addBluetoothTask(taskId, action)
    }



    /**
     * 添加精细搜索任务
     */
    fun addPreciseSearchTask(point: MonitoringPoint, loop: Int, action: suspend () -> BluetoothTaskResult) {
        val taskId = "${MeasureConstants.TASK_PREFIX_PRECISE_SEARCH}${point.pointCode}_LOOP$loop"
        bluetoothQueue.addBluetoothTask(taskId, action)
    }

    /**
     * 添加确认定位任务
     */
    fun addConfirmPositionTask(point: MonitoringPoint, loop: Int, action: suspend () -> BluetoothTaskResult) {
        val taskId = "${MeasureConstants.TASK_PREFIX_CONFIRM_POSITION}${point.pointCode}_LOOP$loop"
        bluetoothQueue.addBluetoothTask(taskId, action)
    }

    /**
     * 添加盘位更改任务
     */
    fun addFaceChangeTask(pointCode: String, loop: Int, isLeft: Boolean, action: suspend () -> BluetoothTaskResult){
        val taskId = "${MeasureConstants.TASK_PREFIX_CHANGE}${pointCode}_LOOP${loop}_" +
                if (isLeft) "LEFT" else "RIGHT"
        bluetoothQueue.addBluetoothTask(taskId, action)
    }

    /**
     * 添加盘位测量任务
     */
    fun addFaceMeasurementTask(pointCode: String, loop: Int, isLeft: Boolean, action: suspend () -> BluetoothTaskResult) {
        val taskId = "${MeasureConstants.TASK_PREFIX_MEASURE}${pointCode}_LOOP${loop}_" +
                if (isLeft) "LEFT" else "RIGHT"
        bluetoothQueue.addBluetoothTask(taskId, action)
    }

    /**
     * 添加定位任务
     */
    fun addPositioningTask(point: MonitoringPoint, loop: Int, action: suspend () -> BluetoothTaskResult) {
        val taskId = "${MeasureConstants.TASK_PREFIX_POSITION}${point.pointCode}_LOOP$loop"
        bluetoothQueue.addBluetoothTask(taskId, action)
    }

    /**
     * 添加单点测量任务
     */
    fun addSingleMeasureTask(pointCode: String, loop: Int, isLeft: Boolean, action: suspend () -> BluetoothTaskResult) {
        val taskId = "${MeasureConstants.TASK_PREFIX_SINGLE_MEASURE}${pointCode}_LOOP${loop}_" +
                if (isLeft) "LEFT" else "RIGHT"
        bluetoothQueue.addBluetoothTask(taskId, action)
    }

    /**
     * 暂停任务队列
     * 当前任务会继续执行，但完成后不会执行下一个任务
     */
    fun pause() {
        if (!bluetoothQueue.isPaused()) {
            bluetoothQueue.pause()
        }
    }

    /**
     * 恢复任务队列执行
     */
    fun resume() {
        if (bluetoothQueue.isPaused()) {
            bluetoothQueue.resume()
        }
    }

    /**
     * 取消所有任务并清空队列
     */
    fun cancelAll() {
        bluetoothQueue.cancelAll()
    }

    /**
     * 获取待执行任务数量
     */
    fun pendingTaskCount() = bluetoothQueue.pendingTaskCount()

    /**
     * 检查队列是否处于暂停状态
     */
    fun isPaused() = bluetoothQueue.isPaused()
}